package com.cmcc.example.face.common.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * <name>CheckOutUtil</name>
 * 判断某些对象是否为空的工具
 * </p >
 *
 * @author arabira
 * @version 1.0 2019-07-15
 * @since 1.0
 */
public class CheckOutUtil {
    public static boolean ListIsEmpty(List list) {
        if (list == null)
            return true;
        else
            return list.isEmpty();
    }

    public static boolean MapIsEmpty(Map map) {
        if (map == null)
            return true;
        else
            return map.isEmpty();
    }

    public static boolean ArrayIsEmpty(Object[] array) {
        if (array == null)
            return true;
        else if (array.length <= 0)
            return true;
        else
            return false;
    }

    /**
     * 检查对象是否为空，若对象中有isEmpty方法则使用该方法判断，否则为true
     * @param o
     * @param className
     * @return
     */
    public static boolean ObjectIsEmpty(Object o, Class className) {
        if (o == null)
            return true;
        else {
            Method[] methods = className.getMethods();
            for (int i = 0; i < methods.length; i++) {
                if (methods[i].getName().equals("isEmpty")) {
                    try {
                        return (boolean) methods[i].invoke(o, null);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
            return true;
        }
    }
}